<?php

require('mc_table.php');

//koneksi ke database
$host = "localhost";
$user = "root";
$pass = "";
$dbnm = "db_rental_dev";
 
$conn = mysql_connect($host, $user, $pass);
if ($conn) {
	$open = mysql_select_db($dbnm);
	if (!$open) {
		die ("Database tidak dapat dibuka karena ".mysql_error());
	}
} else {
	die ("Server MySQL tidak terhubung karena ".mysql_error());
}
//akhir koneksi

//get query dan variabel
$area = $_GET['area'];

//instansiasi fpdf
$pdf = new PDF_MC_Table('L','cm','Legal');
$pdf->AliasNbPages();  
$pdf->AddPage();
$pdf->SetMargins(0.25,1,0.7);  

 #tampilkan judul laporan
$pdf->SetFont('Arial','B','16');
$pdf->Cell(0,0, "CIPAGANTI RENTAL CAR & TRAVEL", 0, 1, 'C');
$pdf->Cell(34.5,2, "STOK KENDARAAN CABANG", 0, 1, 'C');
$pdf->SetFont('Arial','B','12');
$pdf->SetXY(0.8,3);

$sql = "SELECT area FROM gl_area WHERE id = '$area' ";
$cekdata = mysql_query($sql) or die(mysql_error());
$row_data = mysql_fetch_array($cekdata);
$pdf->Cell(8,3, "Area : ".$row_data['area'],0,0,'L');

//membuat kop tabel    
list($namaarea) = mysql_fetch_row(mysql_query("select area from gl_area where id='$area'"));
$pdf->SetFont('Arial','B',11);
$pdf->SetXY(0.25,5);
$pdf->Cell(1,2.3, "No",1,0,'C');
$pdf->Cell(6,2.3, "JENIS KENDARAAN",1,0,'C');

		$str = "select id, area from gl_area ";
		if($area != '') $str .= " where id='$area' ";
		$str .= "order by area asc ";
		$query = mysql_query($str) or die (mysql_error());
		while(list($idarea, $namaarea) = mysql_fetch_row($query)){
		list($jmlcabang) = mysql_fetch_row(mysql_query("select count(*) from gl_gudang where area='$idarea'"));
		$pdf->Cell($jmlcabang*4,0.7, "$namaarea",1,2,'C');
		
		}

		$str = "select id, area from gl_area ";
		if($area != '') $str .= " where id='$area' ";
		$str .= "order by area asc ";
		$query = mysql_query($str) or die (mysql_error());
		while(list($idarea, $namaarea) = mysql_fetch_row($query)){
			$qcb = mysql_query("select id, gudang from gl_gudang where area = '$idarea' order by gudang asc") or die (mysql_error());
			$jqcb = mysql_num_rows($qcb);
			if($jqcb=='0'){
			$pdf->Cell(4,0.6, "",1,0,'C');
			}else{
			while(list($idgudang, $namagudang) = mysql_fetch_row($qcb)){
			$pdf->Cell(4,0.6, "$namagudang",1,0,'C');
			}
			}
		}
		$pdf->ln();
		$pdf->SetFont('Arial','B',9);
		$str = "select id, area from gl_area ";
		if($area != '') $str .= " where id='$area' ";
		$str .= "order by area asc ";
		$query = mysql_query($str) or die (mysql_error());
		while(list($idarea, $namaarea) = mysql_fetch_row($query)){
			$pdf->SetX(7.25);
			$qcb = mysql_query("select id, gudang from gl_gudang where area = '$idarea' order by gudang asc") or die (mysql_error());
			$jqcb = mysql_num_rows($qcb);
			if($jqcb=='0'){
			$pdf->Cell(1,1, "",1,0,'C');
			}else{
			while(list($idgudang, $namagudang) = mysql_fetch_row($qcb)){
			$pdf->Cell(1,1, "CORP",1,0,'C');
			$pdf->Cell(1,1, "RTL",1,0,'C');
			$pdf->Cell(1,1, "TK",1,0,'C');
			$pdf->Cell(1,1, "UGC",1,0,'C');
			
			}
			}
		}
		

$h = 0;
$pdf->SetY(7.3);

$pdf->SetFont('Arial','',9);

//ISI TABEL
$str = "select distinct a.merk, b.merk, a.tipekendaraan, c.tipekendaraan, a.jeniskendaraan, d.jeniskendaraan from gl_masterkendaraan as a left join gl_merkkendaraan as b on (a.merk=b.id) left join gl_tipekendaraan as c on (a.tipekendaraan=c.id) inner join gl_jeniskendaraan as d on (a.jeniskendaraan=d.id) "; //echo $str;
	if($area!='') $str.=" where a.area='$area'"; 
	$str .= " order by a.merk asc "; //echo $str;
	$query = mysql_query($str) or die (mysql_error());
	//print_r($str);
	$no=1;
	while(list($idmerk, $merk, $idtipe, $tipe, $idjenis, $jenis) = mysql_fetch_array($query)){
		$pdf->SetX(0.25);
		/*$pdf->SetWidths(array(1,2,2,2));
		$pdf->SetAligns(Array('C','C','C','C'));
		$pdf->Row(array("$no", "$merk", "$jenis", "$tipe"));*/
		$h = $pdf->GetHeight();
		
	$strsub = "select id, area from gl_area ";
	if($area != '') $strsub .= " where id='$area' ";
	$strsub .= "order by area asc ";
	$querysub = mysql_query($strsub) or die (mysql_error());
	
	while(list($idarea, $namaarea) = mysql_fetch_array($querysub)){
		//$pdf->SetY($h);
		
		$pdf->SetX(7.25);
		
		$qcb = mysql_query("select id, gudang from gl_gudang where area = '$idarea' order by gudang asc") or die (mysql_error());
		$jqcb = mysql_num_rows($qcb);
		if($jqcb=='0'){
		
		$pdf->Cell(1,$h, "",1,0,'C');
		}else{
		while(list($idgudang, $namagudang) = mysql_fetch_array($qcb)){
		$strsub2 = "select count(*) from gl_masterkendaraan where merk='$idmerk' and tipekendaraan='$idtipe' and jeniskendaraan='$idjenis' and unitstok='1' and cabang='$idgudang' ";
		if($area!='') $strsub2.=" and area='$area' ";
		list($jmlretail) = mysql_fetch_row(mysql_query($strsub2)); if($jmlretail==0) $jmlretail="";
		
		$pdf->Cell(1,$h, "$jmlretail",1,0,'C');
		//echo "<td align='center'>$jmlretail</td>";
		$strsub2 = "select count(*) from gl_masterkendaraan where merk='$idmerk' and tipekendaraan='$idtipe' and jeniskendaraan='$idjenis' and unitstok='2' and cabang='$idgudang'";
		if($area!='') $strsub2.=" and area='$area' ";
		list($jmlcorporate) = mysql_fetch_row(mysql_query($strsub2)); if($jmlcorporate==0) $jmlcorporate="";
		
		//echo "<td align='center'>$jmlcorporate</td>";
		$strsub2 = "select count(*) from gl_masterkendaraan where merk='$idmerk' and tipekendaraan='$idtipe' and jeniskendaraan='$idjenis' and unitstok='3' and cabang='$idgudang' ";
		if($area!='') $strsub2.=" and area='$area' ";
		list($jmloperational) = mysql_fetch_row(mysql_query($strsub2)); if($jmloperational==0) $jmloperational="";
		
		//echo "<td align='center'>$jmloperational</td>";
		$strsub2 = "select count(*) from gl_masterkendaraan where merk='$idmerk' and tipekendaraan='$idtipe' and jeniskendaraan='$idjenis' and unitstok='4' and cabang='$idgudang' ";
		if($area!='') $strsub2.=" and area='$area' ";
		list($jmlcargo) = mysql_fetch_row(mysql_query($strsub2)); if($jmlcargo==0) $jmlcargo="";
		
		//echo "<td align='center'>$jmlcargo</td>";
		
		}
		}
		
		
		
	}
	
	$no++;
	
	}
	
	

$pdf->Output('Laporan Stok kendaraan Cabang '.$row_data['area'].'.pdf','D');


?>